package com.feizhoutuobang.shapeline.util.image;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import com.feizhoutuobang.shapeline.util.WebClient;
import com.sun.image.codec.jpeg.ImageFormatException;

public abstract class NetImage {

	private String url = null;
	private int maxWidth = 0;
	private int maxHeight = 0;
	private boolean colored = true;
	private WebClient webClient;

	public NetImage(String url) {
		this.url = url;
	}
	
	public NetImage() {
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public int getMaxWidth() {
		return maxWidth;
	}

	public void setMaxWidth(int maxWidth) {
		this.maxWidth = maxWidth;
	}

	public int getMaxHeight() {
		return maxHeight;
	}

	public boolean isColored() {
		return colored;
	}

	public void setColored(boolean colored) {
		this.colored = colored;
	}

	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
	}

	/**
	 * 设立该抽象方法的意图是要使具体子类实现不同格式图片的编码方法(jpeg等)
	 */
	protected abstract void encode(FileOutputStream out, BufferedImage bufferedImage) 
		throws ImageFormatException, IOException;
	
	/**
	 * 取得原图片
	 */
	public final File getImage(String newFilePath) throws MalformedURLException, IOException{
		if (url == null) return null;
		
		InputStream stream = null;
		try {
			stream = getWebClient().downloadAsStream(url);
			Image image = javax.imageio.ImageIO.read(stream);
			
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			if (getMaxWidth() > 0 && width > getMaxWidth()) {
				height = height * getMaxWidth() / width;
				width = getMaxWidth();
			}
			if (getMaxHeight() > 0 && height > getMaxHeight()) {
				width = width * getMaxHeight() / height;
				height = getMaxHeight();
			}
			
			int imageType = BufferedImage.TYPE_BYTE_GRAY;
			if (colored) {
				imageType = BufferedImage.TYPE_INT_RGB;
			}
			FileOutputStream out = null;
			try {
				BufferedImage bufferedImage = new BufferedImage(width, height, imageType);
				bufferedImage.getGraphics().drawImage(image, 0, 0, width, height,null);
				out = new FileOutputStream(newFilePath);
				encode(out, bufferedImage);
				
				return new File(newFilePath);
			}
			finally {
				try {out.close(); } catch (Exception e) {}
			}
		}
		finally {
			try {stream.close(); } catch (Exception e) {}
			try {getWebClient().release(); } catch (Exception e) {}
		}
	}
	
	/**
	 * 直接指定网络图片的位置，和下载到本地文件系统的位置
	 */
	public File getImageFromUrl(String url , String newFilePath) throws MalformedURLException, IOException{
		this.setUrl(url);
		return this.getImage(newFilePath);
	}

	public void setWebClient(WebClient webClient) {
		this.webClient = webClient;
	}

	public WebClient getWebClient() {
		return webClient;
	}
}
